<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>ipaddress.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_ipaddress.js.html">ipaddress.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This file contains classes to handle IPv4 and IPv6 addresses.
<a name="line17"></a> * This implementation is mostly based on Google&#39;s project:
<a name="line18"></a> * http://code.google.com/p/ipaddr-py/.
<a name="line19"></a> *
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.net.IpAddress&#39;);
<a name="line23"></a>goog.provide(&#39;goog.net.Ipv4Address&#39;);
<a name="line24"></a>goog.provide(&#39;goog.net.Ipv6Address&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.array&#39;);
<a name="line27"></a>goog.require(&#39;goog.math.Integer&#39;);
<a name="line28"></a>goog.require(&#39;goog.object&#39;);
<a name="line29"></a>goog.require(&#39;goog.string&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Abstract class defining an IP Address.
<a name="line35"></a> *
<a name="line36"></a> * Please use goog.net.IpAddress static methods or
<a name="line37"></a> * goog.net.Ipv4Address/Ipv6Address classes.
<a name="line38"></a> *
<a name="line39"></a> * @param {!goog.math.Integer} address The Ip Address.
<a name="line40"></a> * @param {number} version The version number (4, 6).
<a name="line41"></a> * @constructor
<a name="line42"></a> */
<a name="line43"></a>goog.net.IpAddress = function(address, version) {
<a name="line44"></a>  /**
<a name="line45"></a>   * The IP Address.
<a name="line46"></a>   * @type {!goog.math.Integer}
<a name="line47"></a>   * @private
<a name="line48"></a>   */
<a name="line49"></a>  this.ip_ = address;
<a name="line50"></a>
<a name="line51"></a>  /**
<a name="line52"></a>   * The IP Address version.
<a name="line53"></a>   * @type {number}
<a name="line54"></a>   * @private
<a name="line55"></a>   */
<a name="line56"></a>  this.version_ = version;
<a name="line57"></a>
<a name="line58"></a>  /**
<a name="line59"></a>   * The IPAddress, as string.
<a name="line60"></a>   * @type {string}
<a name="line61"></a>   * @private
<a name="line62"></a>   */
<a name="line63"></a>  this.ipStr_ = &#39;&#39;;
<a name="line64"></a>};
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * @return {number} The IP Address version.
<a name="line69"></a> */
<a name="line70"></a>goog.net.IpAddress.prototype.getVersion = function() {
<a name="line71"></a>  return this.version_;
<a name="line72"></a>};
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>/**
<a name="line76"></a> * @param {!goog.net.IpAddress} other The other IP Address.
<a name="line77"></a> * @return {boolean} true if the IP Addresses are equal.
<a name="line78"></a> */
<a name="line79"></a>goog.net.IpAddress.prototype.equals = function(other) {
<a name="line80"></a>  return (this.version_ == other.getVersion() &amp;&amp;
<a name="line81"></a>          this.ip_.equals(other.toInteger()));
<a name="line82"></a>};
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * @return {goog.math.Integer} The IP Address, as an Integer.
<a name="line87"></a> */
<a name="line88"></a>goog.net.IpAddress.prototype.toInteger = function() {
<a name="line89"></a>  return /** @type {goog.math.Integer} */ (goog.object.clone(this.ip_));
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * @return {string} The IP Address, as an URI string following RFC 3986.
<a name="line95"></a> */
<a name="line96"></a>goog.net.IpAddress.prototype.toUriString = goog.abstractMethod;
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * @return {string} The IP Address, as a string.
<a name="line101"></a> * @override
<a name="line102"></a> */
<a name="line103"></a>goog.net.IpAddress.prototype.toString = goog.abstractMethod;
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Parses an IP Address in a string.
<a name="line108"></a> * If the string is malformed, the function will simply return null
<a name="line109"></a> * instead of raising an exception.
<a name="line110"></a> *
<a name="line111"></a> * @param {string} address The IP Address.
<a name="line112"></a> * @see {goog.net.Ipv4Address}
<a name="line113"></a> * @see {goog.net.Ipv6Address}
<a name="line114"></a> * @return {goog.net.IpAddress} The IP Address or null.
<a name="line115"></a> */
<a name="line116"></a>goog.net.IpAddress.fromString = function(address) {
<a name="line117"></a>  try {
<a name="line118"></a>    if (address.indexOf(&#39;:&#39;) != -1) {
<a name="line119"></a>      return new goog.net.Ipv6Address(address);
<a name="line120"></a>    }
<a name="line121"></a>
<a name="line122"></a>    return new goog.net.Ipv4Address(address);
<a name="line123"></a>  } catch (e) {
<a name="line124"></a>    // Both constructors raise exception if the address is malformed (ie.
<a name="line125"></a>    // invalid). The user of this function should not care about catching
<a name="line126"></a>    // the exception, espcially if it&#39;s used to validate an user input.
<a name="line127"></a>    return null;
<a name="line128"></a>  }
<a name="line129"></a>};
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Tries to parse a string represented as a host portion of an URI.
<a name="line134"></a> * See RFC 3986 for more details on IPv6 addresses inside URI.
<a name="line135"></a> * If the string is malformed, the function will simply return null
<a name="line136"></a> * instead of raising an exception.
<a name="line137"></a> *
<a name="line138"></a> * @param {string} address A RFC 3986 encoded IP address.
<a name="line139"></a> * @see {goog.net.Ipv4Address}
<a name="line140"></a> * @see {goog.net.Ipv6Address}
<a name="line141"></a> * @return {goog.net.IpAddress} The IP Address.
<a name="line142"></a> */
<a name="line143"></a>goog.net.IpAddress.fromUriString = function(address) {
<a name="line144"></a>  try {
<a name="line145"></a>    if (goog.string.startsWith(address, &#39;[&#39;) &amp;&amp;
<a name="line146"></a>        goog.string.endsWith(address, &#39;]&#39;)) {
<a name="line147"></a>      return new goog.net.Ipv6Address(
<a name="line148"></a>          address.substring(1, address.length - 1));
<a name="line149"></a>    }
<a name="line150"></a>
<a name="line151"></a>    return new goog.net.Ipv4Address(address);
<a name="line152"></a>  } catch (e) {
<a name="line153"></a>    // Both constructors raise exception if the address is malformed (ie.
<a name="line154"></a>    // invalid). The user of this function should not care about catching
<a name="line155"></a>    // the exception, espcially if it&#39;s used to validate an user input.
<a name="line156"></a>    return null;
<a name="line157"></a>  }
<a name="line158"></a>};
<a name="line159"></a>
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/**
<a name="line163"></a> * Takes a string or a number and returns a IPv4 Address.
<a name="line164"></a> *
<a name="line165"></a> * This constructor accepts strings and instance of goog.math.Integer.
<a name="line166"></a> * If you pass a goog.math.Integer, make sure that its sign is set to positive.
<a name="line167"></a> * @param {(string|!goog.math.Integer)} address The address to store.
<a name="line168"></a> * @extends {goog.net.IpAddress}
<a name="line169"></a> * @constructor
<a name="line170"></a> */
<a name="line171"></a>goog.net.Ipv4Address = function(address) {
<a name="line172"></a>  var ip = goog.math.Integer.ZERO;
<a name="line173"></a>  if (address instanceof goog.math.Integer) {
<a name="line174"></a>    if (address.getSign() != 0 ||
<a name="line175"></a>        address.lessThan(goog.math.Integer.ZERO) ||
<a name="line176"></a>        address.greaterThan(goog.net.Ipv4Address.MAX_ADDRESS_)) {
<a name="line177"></a>      throw Error(&#39;The address does not look like an IPv4.&#39;);
<a name="line178"></a>    } else {
<a name="line179"></a>      ip = goog.object.clone(address);
<a name="line180"></a>    }
<a name="line181"></a>  } else {
<a name="line182"></a>    if (!goog.net.Ipv4Address.REGEX_.test(address)) {
<a name="line183"></a>      throw Error(address + &#39; does not look like an IPv4 address.&#39;);
<a name="line184"></a>    }
<a name="line185"></a>
<a name="line186"></a>    var octets = address.split(&#39;.&#39;);
<a name="line187"></a>    if (octets.length != 4) {
<a name="line188"></a>      throw Error(address + &#39; does not look like an IPv4 address.&#39;);
<a name="line189"></a>    }
<a name="line190"></a>
<a name="line191"></a>    for (var i = 0; i &lt; octets.length; i++) {
<a name="line192"></a>      var parsedOctet = goog.string.toNumber(octets[i]);
<a name="line193"></a>      if (isNaN(parsedOctet) ||
<a name="line194"></a>          parsedOctet &lt; 0 || parsedOctet &gt; 255 ||
<a name="line195"></a>          (octets[i].length != 1 &amp;&amp; goog.string.startsWith(octets[i], &#39;0&#39;))) {
<a name="line196"></a>        throw Error(&#39;In &#39; + address + &#39;, octet &#39; + i + &#39; is not valid&#39;);
<a name="line197"></a>      }
<a name="line198"></a>      var intOctet = goog.math.Integer.fromNumber(parsedOctet);
<a name="line199"></a>      ip = ip.shiftLeft(8).or(intOctet);
<a name="line200"></a>    }
<a name="line201"></a>  }
<a name="line202"></a>  goog.base(this, /** @type {!goog.math.Integer} */ (ip), 4);
<a name="line203"></a>};
<a name="line204"></a>goog.inherits(goog.net.Ipv4Address, goog.net.IpAddress);
<a name="line205"></a>
<a name="line206"></a>
<a name="line207"></a>/**
<a name="line208"></a> * Regular expression matching all the allowed chars for IPv4.
<a name="line209"></a> * @type {RegExp}
<a name="line210"></a> * @private
<a name="line211"></a> * @const
<a name="line212"></a> */
<a name="line213"></a>goog.net.Ipv4Address.REGEX_ = /^[0-9.]*$/;
<a name="line214"></a>
<a name="line215"></a>
<a name="line216"></a>/**
<a name="line217"></a> * The Maximum length for a netmask (aka, the number of bits for IPv4).
<a name="line218"></a> * @type {number}
<a name="line219"></a> * @const
<a name="line220"></a> */
<a name="line221"></a>goog.net.Ipv4Address.MAX_NETMASK_LENGTH = 32;
<a name="line222"></a>
<a name="line223"></a>
<a name="line224"></a>/**
<a name="line225"></a> * The Maximum address possible for IPv4.
<a name="line226"></a> * @type {goog.math.Integer}
<a name="line227"></a> * @private
<a name="line228"></a> * @const
<a name="line229"></a> */
<a name="line230"></a>goog.net.Ipv4Address.MAX_ADDRESS_ = goog.math.Integer.ONE.shiftLeft(
<a name="line231"></a>    goog.net.Ipv4Address.MAX_NETMASK_LENGTH).subtract(goog.math.Integer.ONE);
<a name="line232"></a>
<a name="line233"></a>
<a name="line234"></a>/**
<a name="line235"></a> * @override
<a name="line236"></a> */
<a name="line237"></a>goog.net.Ipv4Address.prototype.toString = function() {
<a name="line238"></a>  if (this.ipStr_) {
<a name="line239"></a>    return this.ipStr_;
<a name="line240"></a>  }
<a name="line241"></a>
<a name="line242"></a>  var ip = this.ip_.getBitsUnsigned(0);
<a name="line243"></a>  var octets = [];
<a name="line244"></a>  for (var i = 3; i &gt;= 0; i--) {
<a name="line245"></a>    octets[i] = String((ip &amp; 0xff));
<a name="line246"></a>    ip = ip &gt;&gt;&gt; 8;
<a name="line247"></a>  }
<a name="line248"></a>
<a name="line249"></a>  this.ipStr_ = octets.join(&#39;.&#39;);
<a name="line250"></a>
<a name="line251"></a>  return this.ipStr_;
<a name="line252"></a>};
<a name="line253"></a>
<a name="line254"></a>
<a name="line255"></a>/**
<a name="line256"></a> * @override
<a name="line257"></a> */
<a name="line258"></a>goog.net.Ipv4Address.prototype.toUriString = function() {
<a name="line259"></a>  return this.toString();
<a name="line260"></a>};
<a name="line261"></a>
<a name="line262"></a>
<a name="line263"></a>
<a name="line264"></a>/**
<a name="line265"></a> * Takes a string or a number and returns an IPv6 Address.
<a name="line266"></a> *
<a name="line267"></a> * This constructor accepts strings and instance of goog.math.Integer.
<a name="line268"></a> * If you pass a goog.math.Integer, make sure that its sign is set to positive.
<a name="line269"></a> * @param {(string|!goog.math.Integer)} address The address to store.
<a name="line270"></a> * @constructor
<a name="line271"></a> * @extends {goog.net.IpAddress}
<a name="line272"></a> */
<a name="line273"></a>goog.net.Ipv6Address = function(address) {
<a name="line274"></a>  var ip = goog.math.Integer.ZERO;
<a name="line275"></a>  if (address instanceof goog.math.Integer) {
<a name="line276"></a>    if (address.getSign() != 0 ||
<a name="line277"></a>        address.lessThan(goog.math.Integer.ZERO) ||
<a name="line278"></a>        address.greaterThan(goog.net.Ipv6Address.MAX_ADDRESS_)) {
<a name="line279"></a>      throw Error(&#39;The address does not look like a valid IPv6.&#39;);
<a name="line280"></a>    } else {
<a name="line281"></a>      ip = goog.object.clone(address);
<a name="line282"></a>    }
<a name="line283"></a>  } else {
<a name="line284"></a>    if (!goog.net.Ipv6Address.REGEX_.test(address)) {
<a name="line285"></a>      throw Error(address + &#39; is not a valid IPv6 address.&#39;);
<a name="line286"></a>    }
<a name="line287"></a>
<a name="line288"></a>    var splitColon = address.split(&#39;:&#39;);
<a name="line289"></a>    if (splitColon[splitColon.length - 1].indexOf(&#39;.&#39;) != -1) {
<a name="line290"></a>      var newHextets = goog.net.Ipv6Address.dottedQuadtoHextets_(
<a name="line291"></a>          splitColon[splitColon.length - 1]);
<a name="line292"></a>      goog.array.removeAt(splitColon, splitColon.length - 1);
<a name="line293"></a>      goog.array.extend(splitColon, newHextets);
<a name="line294"></a>      address = splitColon.join(&#39;:&#39;);
<a name="line295"></a>    }
<a name="line296"></a>
<a name="line297"></a>    var splitDoubleColon = address.split(&#39;::&#39;);
<a name="line298"></a>    if (splitDoubleColon.length &gt; 2 ||
<a name="line299"></a>        (splitDoubleColon.length == 1 &amp;&amp; splitColon.length != 8)) {
<a name="line300"></a>      throw Error(address + &#39; is not a valid IPv6 address.&#39;);
<a name="line301"></a>    }
<a name="line302"></a>
<a name="line303"></a>    var ipArr;
<a name="line304"></a>    if (splitDoubleColon.length &gt; 1) {
<a name="line305"></a>      ipArr = goog.net.Ipv6Address.explode_(splitDoubleColon);
<a name="line306"></a>    } else {
<a name="line307"></a>      ipArr = splitColon;
<a name="line308"></a>    }
<a name="line309"></a>
<a name="line310"></a>    if (ipArr.length != 8) {
<a name="line311"></a>      throw Error(address + &#39; is not a valid IPv6 address&#39;);
<a name="line312"></a>    }
<a name="line313"></a>
<a name="line314"></a>    for (var i = 0; i &lt; ipArr.length; i++) {
<a name="line315"></a>      var parsedHextet = goog.math.Integer.fromString(ipArr[i], 16);
<a name="line316"></a>      if (parsedHextet.lessThan(goog.math.Integer.ZERO) ||
<a name="line317"></a>          parsedHextet.greaterThan(goog.net.Ipv6Address.MAX_HEXTET_VALUE_)) {
<a name="line318"></a>        throw Error(ipArr[i] + &#39; in &#39; + address + &#39; is not a valid hextet.&#39;);
<a name="line319"></a>      }
<a name="line320"></a>      ip = ip.shiftLeft(16).or(parsedHextet);
<a name="line321"></a>    }
<a name="line322"></a>  }
<a name="line323"></a>  goog.base(this, /** @type {!goog.math.Integer} */ (ip), 6);
<a name="line324"></a>};
<a name="line325"></a>goog.inherits(goog.net.Ipv6Address, goog.net.IpAddress);
<a name="line326"></a>
<a name="line327"></a>
<a name="line328"></a>/**
<a name="line329"></a> * Regular expression matching all allowed chars for an IPv6.
<a name="line330"></a> * @type {RegExp}
<a name="line331"></a> * @private
<a name="line332"></a> * @const
<a name="line333"></a> */
<a name="line334"></a>goog.net.Ipv6Address.REGEX_ = /^([a-fA-F0-9]*:){2}[a-fA-F0-9:.]*$/;
<a name="line335"></a>
<a name="line336"></a>
<a name="line337"></a>/**
<a name="line338"></a> * The Maximum length for a netmask (aka, the number of bits for IPv6).
<a name="line339"></a> * @type {number}
<a name="line340"></a> * @const
<a name="line341"></a> */
<a name="line342"></a>goog.net.Ipv6Address.MAX_NETMASK_LENGTH = 128;
<a name="line343"></a>
<a name="line344"></a>
<a name="line345"></a>/**
<a name="line346"></a> * The maximum value of a hextet.
<a name="line347"></a> * @type {goog.math.Integer}
<a name="line348"></a> * @private
<a name="line349"></a> * @const
<a name="line350"></a> */
<a name="line351"></a>goog.net.Ipv6Address.MAX_HEXTET_VALUE_ = goog.math.Integer.fromInt(65535);
<a name="line352"></a>
<a name="line353"></a>
<a name="line354"></a>/**
<a name="line355"></a> * The Maximum address possible for IPv6.
<a name="line356"></a> * @type {goog.math.Integer}
<a name="line357"></a> * @private
<a name="line358"></a> * @const
<a name="line359"></a> */
<a name="line360"></a>goog.net.Ipv6Address.MAX_ADDRESS_ = goog.math.Integer.ONE.shiftLeft(
<a name="line361"></a>    goog.net.Ipv6Address.MAX_NETMASK_LENGTH).subtract(goog.math.Integer.ONE);
<a name="line362"></a>
<a name="line363"></a>
<a name="line364"></a>/**
<a name="line365"></a> * @override
<a name="line366"></a> */
<a name="line367"></a>goog.net.Ipv6Address.prototype.toString = function() {
<a name="line368"></a>  if (this.ipStr_) {
<a name="line369"></a>    return this.ipStr_;
<a name="line370"></a>  }
<a name="line371"></a>
<a name="line372"></a>  var outputArr = [];
<a name="line373"></a>  for (var i = 3; i &gt;= 0; i--) {
<a name="line374"></a>    var bits = this.ip_.getBitsUnsigned(i);
<a name="line375"></a>    var firstHextet = bits &gt;&gt;&gt; 16;
<a name="line376"></a>    var secondHextet = bits &amp; 0xffff;
<a name="line377"></a>    outputArr.push(firstHextet.toString(16));
<a name="line378"></a>    outputArr.push(secondHextet.toString(16));
<a name="line379"></a>  }
<a name="line380"></a>
<a name="line381"></a>  outputArr = goog.net.Ipv6Address.compress_(outputArr);
<a name="line382"></a>  this.ipStr_ = outputArr.join(&#39;:&#39;);
<a name="line383"></a>  return this.ipStr_;
<a name="line384"></a>};
<a name="line385"></a>
<a name="line386"></a>
<a name="line387"></a>/**
<a name="line388"></a> * @override
<a name="line389"></a> */
<a name="line390"></a>goog.net.Ipv6Address.prototype.toUriString = function() {
<a name="line391"></a>  return &#39;[&#39; + this.toString() + &#39;]&#39;;
<a name="line392"></a>};
<a name="line393"></a>
<a name="line394"></a>
<a name="line395"></a>/**
<a name="line396"></a> * This method is in charge of expanding/exploding an IPv6 string from its
<a name="line397"></a> * compressed form.
<a name="line398"></a> * @private
<a name="line399"></a> * @param {!Array.&lt;string&gt;} address An IPv6 address split around &#39;::&#39;.
<a name="line400"></a> * @return {Array.&lt;string&gt;} The expanded version of the IPv6.
<a name="line401"></a> */
<a name="line402"></a>goog.net.Ipv6Address.explode_ = function(address) {
<a name="line403"></a>  var basePart = address[0].split(&#39;:&#39;);
<a name="line404"></a>  var secondPart = address[1].split(&#39;:&#39;);
<a name="line405"></a>
<a name="line406"></a>  if (basePart.length == 1 &amp;&amp; basePart[0] == &#39;&#39;) {
<a name="line407"></a>    basePart = [];
<a name="line408"></a>  }
<a name="line409"></a>  if (secondPart.length == 1 &amp;&amp; secondPart[0] == &#39;&#39;) {
<a name="line410"></a>    secondPart = [];
<a name="line411"></a>  }
<a name="line412"></a>
<a name="line413"></a>  // Now we fill the gap with 0.
<a name="line414"></a>  var gap = 8 - (basePart.length + secondPart.length);
<a name="line415"></a>
<a name="line416"></a>  if (gap &lt; 1) {
<a name="line417"></a>    return [];
<a name="line418"></a>  }
<a name="line419"></a>
<a name="line420"></a>  goog.array.extend(basePart, goog.array.repeat(&#39;0&#39;, gap));
<a name="line421"></a>
<a name="line422"></a>  // Now we merge the basePart + gap + secondPart
<a name="line423"></a>  goog.array.extend(basePart, secondPart);
<a name="line424"></a>
<a name="line425"></a>  return basePart;
<a name="line426"></a>};
<a name="line427"></a>
<a name="line428"></a>
<a name="line429"></a>/**
<a name="line430"></a> * This method is in charge of compressing an expanded IPv6 array of hextets.
<a name="line431"></a> * @private
<a name="line432"></a> * @param {!Array.&lt;string&gt;} hextets The array of hextet.
<a name="line433"></a> * @return {Array.&lt;string&gt;} The compressed version of this array.
<a name="line434"></a> */
<a name="line435"></a>goog.net.Ipv6Address.compress_ = function(hextets) {
<a name="line436"></a>  var bestStart = -1;
<a name="line437"></a>  var start = -1;
<a name="line438"></a>  var bestSize = 0;
<a name="line439"></a>  var size = 0;
<a name="line440"></a>  for (var i = 0; i &lt; hextets.length; i++) {
<a name="line441"></a>    if (hextets[i] == &#39;0&#39;) {
<a name="line442"></a>      size++;
<a name="line443"></a>      if (start == -1) {
<a name="line444"></a>        start = i;
<a name="line445"></a>      }
<a name="line446"></a>      if (size &gt; bestSize) {
<a name="line447"></a>        bestSize = size;
<a name="line448"></a>        bestStart = start;
<a name="line449"></a>      }
<a name="line450"></a>    } else {
<a name="line451"></a>      start = -1;
<a name="line452"></a>      size = 0;
<a name="line453"></a>    }
<a name="line454"></a>  }
<a name="line455"></a>
<a name="line456"></a>  if (bestSize &gt; 0) {
<a name="line457"></a>    if ((bestStart + bestSize) == hextets.length) {
<a name="line458"></a>      hextets.push(&#39;&#39;);
<a name="line459"></a>    }
<a name="line460"></a>    hextets.splice(bestStart, bestSize, &#39;&#39;);
<a name="line461"></a>
<a name="line462"></a>    if (bestStart == 0) {
<a name="line463"></a>      hextets = [&#39;&#39;].concat(hextets);
<a name="line464"></a>    }
<a name="line465"></a>  }
<a name="line466"></a>  return hextets;
<a name="line467"></a>};
<a name="line468"></a>
<a name="line469"></a>
<a name="line470"></a>/**
<a name="line471"></a> * This method will convert an IPv4 to a list of 2 hextets.
<a name="line472"></a> *
<a name="line473"></a> * For instance, 1.2.3.4 will be converted to [&#39;0102&#39;, &#39;0304&#39;].
<a name="line474"></a> * @private
<a name="line475"></a> * @param {string} quads An IPv4 as a string.
<a name="line476"></a> * @return {Array.&lt;string&gt;} A list of 2 hextets.
<a name="line477"></a> */
<a name="line478"></a>goog.net.Ipv6Address.dottedQuadtoHextets_ = function(quads) {
<a name="line479"></a>  var ip4 = new goog.net.Ipv4Address(quads).toInteger();
<a name="line480"></a>  var bits = ip4.getBitsUnsigned(0);
<a name="line481"></a>  var hextets = [];
<a name="line482"></a>
<a name="line483"></a>  hextets.push(((bits &gt;&gt;&gt; 16) &amp; 0xffff).toString(16));
<a name="line484"></a>  hextets.push((bits &amp; 0xffff).toString(16));
<a name="line485"></a>
<a name="line486"></a>  return hextets;
<a name="line487"></a>};
<a name="line488"></a>
<a name="line489"></a>
<a name="line490"></a>/**
<a name="line491"></a> * @return {boolean} true if the IPv6 contains a mapped IPv4.
<a name="line492"></a> */
<a name="line493"></a>goog.net.Ipv6Address.prototype.isMappedIpv4Address = function() {
<a name="line494"></a>  return (this.ip_.getBitsUnsigned(3) == 0 &amp;&amp;
<a name="line495"></a>          this.ip_.getBitsUnsigned(2) == 0 &amp;&amp;
<a name="line496"></a>          this.ip_.getBitsUnsigned(1) == 0xffff);
<a name="line497"></a>};
<a name="line498"></a>
<a name="line499"></a>
<a name="line500"></a>/**
<a name="line501"></a> * Will return the mapped IPv4 address in this IPv6 address.
<a name="line502"></a> * @return {goog.net.Ipv4Address} an IPv4 or null.
<a name="line503"></a> */
<a name="line504"></a>goog.net.Ipv6Address.prototype.getMappedIpv4Address = function() {
<a name="line505"></a>  if (!this.isMappedIpv4Address()) {
<a name="line506"></a>    return null;
<a name="line507"></a>  }
<a name="line508"></a>
<a name="line509"></a>  var newIpv4 = new goog.math.Integer([this.ip_.getBitsUnsigned(0)], 0);
<a name="line510"></a>  return new goog.net.Ipv4Address(newIpv4);
<a name="line511"></a>};
</pre>


</body>
</html>
